import 'package:flutter/material.dart';
import '../models/member.dart';
import 'package:provider/provider.dart';
import '../providers/member_provider.dart';
import '../providers/auth_provider.dart';

class AddMemberDialog extends StatefulWidget {
  const AddMemberDialog({super.key});

  @override
  State<AddMemberDialog> createState() => _AddMemberDialogState();
}

class _AddMemberDialogState extends State<AddMemberDialog> {
  final _formKey = GlobalKey<FormState>();
  final _nameController = TextEditingController();
  final _emailController = TextEditingController();
  MemberRole _selectedRole = MemberRole.member;

  @override
  Widget build(BuildContext context) {
    final bool isAdmin = context.watch<AuthProvider>().isAdmin;
    if (!isAdmin) {
      Navigator.of(context).pop();
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('您没有权限添加团队成员')),
      );
      return const SizedBox.shrink();
    }

    return AlertDialog(
      title: const Text('添加新成员'),
      content: Form(
        key: _formKey,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextFormField(
              controller: _nameController,
              decoration: const InputDecoration(
                labelText: '姓名',
                icon: Icon(Icons.person),
              ),
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return '请输入姓名';
                }
                return null;
              },
            ),
            TextFormField(
              controller: _emailController,
              decoration: const InputDecoration(
                labelText: '邮箱',
                icon: Icon(Icons.email),
              ),
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return '请输入邮箱';
                }
                if (!value.contains('@')) {
                  return '请输入有效的邮箱地址';
                }
                return null;
              },
            ),
            const SizedBox(height: 16),
            DropdownButtonFormField<MemberRole>(
              value: _selectedRole,
              decoration: const InputDecoration(
                labelText: '角色',
                icon: Icon(Icons.admin_panel_settings),
              ),
              items: [
                DropdownMenuItem(
                  value: MemberRole.admin,
                  child: const Text('管理员'),
                ),
                DropdownMenuItem(
                  value: MemberRole.member,
                  child: const Text('成员'),
                ),
                DropdownMenuItem(
                  value: MemberRole.guest,
                  child: const Text('访客'),
                ),
              ],
              onChanged: (value) {
                setState(() {
                  _selectedRole = value!;
                });
              },
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('取消'),
        ),
        TextButton(
          onPressed: _submitForm,
          child: const Text('添加'),
        ),
      ],
    );
  }

  void _submitForm() {
    if (_formKey.currentState!.validate()) {
      final memberProvider = Provider.of<MemberProvider>(context, listen: false);
      memberProvider.addMember(
        _nameController.text,
        _emailController.text,
        _selectedRole,
      ).then((_) {
        Navigator.pop(context);
      }).catchError((error) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('添加成员失败: $error')),
        );
      });
    }
  }

  @override
  void dispose() {
    _nameController.dispose();
    _emailController.dispose();
    super.dispose();
  }
} 